package com.kzd.gtgame.main.game

import android.Manifest
import android.graphics.BitmapFactory
import com.kzd.gtgame.R
import com.kzd.gtgame.base.BaseViewModel
import com.kzd.gtgame.entity.GameVip
import com.kzd.gtgame.main.KEY_GAME_DETAILS_TITLE
import com.kzd.gtgame.main.KEY_GAME_DETAILS_VIP
import com.kzd.gtgame.main.KEY_GAME_DETAILS_VIP_STR
import com.kzd.gtgame.main.game.adapter.GameVipAdapter
import com.kzd.gtgame.main.popup.ServiceCodePopup
import com.kzd.gtgame.utils.JsonUtils
import com.kzd.gtgame.utils.saveImageToGallery
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_game_vip.*

class GameVipActivity : OwnerViewModeActivity<BaseViewModel>() {

    private val mServiceCodePopup: ServiceCodePopup by lazy {
        ServiceCodePopup(context = this) { _, type ->
            viewModel.userPermissions(this@GameVipActivity, { isPermissions ->
                if (!isPermissions) {
                    toast("请到设置中开启相关权限")
                    return@userPermissions
                }
                val bitmap = BitmapFactory.decodeResource(resources,
                    if (type == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007)
                val fileName = "${if (type == 1) R.mipmap.ic_40008 else R.mipmap.ic_40007}.png"
                if (saveImageToGallery(this@GameVipActivity, bitmap, fileName)) {
                    toast("已保存")
                } else {
                    toast("保存失败")
                }
            }, Manifest.permission.WRITE_EXTERNAL_STORAGE,
                Manifest.permission.READ_EXTERNAL_STORAGE)
        }
    }

    private val mGameVipAdapter = GameVipAdapter()

    private val mTitle: String
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_TITLE) ?: ""

    private val mGameVip: MutableList<GameVip>
        get() = try {
            val vipStr = intent?.extras?.getString(KEY_GAME_DETAILS_VIP)
                ?: throw Exception()
            JsonUtils.stringToList(vipStr, GameVip::class.java)
        } catch (e: Exception) {
            mutableListOf()
        }

    private val mVipStr: String?
        get() = intent?.extras?.getString(KEY_GAME_DETAILS_VIP_STR)

    override fun viewModel(): BaseViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_game_vip

    override fun stateBar(): StatusBar {
        return StatusBar(view = flGameVipBar)
    }

    override fun initView() {
        flGameVipBar.setTitleTextContent(mTitle)
        rvGameVipBody.adapter = mGameVipAdapter

        flGameVipBar.setOnClickRightTextView {
//            toExternalAppQQ(activity = this)
            val image = viewModel.userServiceLiveData.value
                ?.data?.data?.qy_wx_img
            mServiceCodePopup.setContent(if (image.isNullOrEmpty()) {
                R.mipmap.ic_40008
            } else image, 1)
                .showPopupWindow()
        }

    }

    override fun initData() {
        mGameVipAdapter.setNewInstance(mGameVip)
        mVipStr?.let { tvGameVipHint.text = it }
        viewModel.userService()
    }
}